Skip to content

bpo-37953:Improve ForwardRef equality checks#15628

Closed
hongweipeng wants to merge 4 commits into
python:masterfrom
hongweipeng:issue37953_forwardref_equal
Closed

bpo-37953:Improve ForwardRef equality checks#15628
hongweipeng wants to merge 4 commits into
python:masterfrom
hongweipeng:issue37953_forwardref_equal

Conversation

@hongweipeng

@hongweipeng hongweipeng commented Aug 31, 2019

Copy link
Copy Markdown
Contributor

@brandtbucher brandtbucher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a good change. I found one optimization that can be made, below.

Can you also add regression tests as well?

Comment thread Lib/typing.py Outdated
if p not in new_params:
new_params.append(p)
params = new_params
return tuple(params)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole bit can be sped up by just replacing it with:

return tuple(dict.fromkeys(params))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will add some tests. By the way, params is a list, tuple(dict.fromkeys(params)) seems not faster than tuple(params), I think it would be better to keep tuple(params):

20190901004654

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I should have been clearer! I meant replacing this whole list + loop bit with that one line. dict.fromkeys is a nice shortcut to remove duplicates while preserving order.

@brandtbucher brandtbucher Sep 1, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... I see now. You're trying to avoid the (broken) hashing.

I don't know how I feel about that. I think hashing should be fixed rather than just sidestepped here, since it's so tied to equality. At this point I'm actually in favor of a hybrid approach: combining your fix for __eq__ and the other PR #15400’s fix for __hash__.

@gvanrossum and @ilevkivskyi, thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hongweipeng you can revert your most recent commit (3676a50, “code review”). My comment was misguided!

Comment thread Lib/typing.py
params = new_params
assert not all_params, all_params
return tuple(params)
return tuple(dict.fromkeys(params))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH this looks like a hack. If at some point we would make sets ordered the same way as dicts are, it would be good to update the code, but currently I think I like the old way more.

@ilevkivskyi

Copy link
Copy Markdown
Member

See also my comment #15400 (comment) in the other PR.

@hongweipeng

Copy link
Copy Markdown
Contributor Author

PR #15650 is better.

@hongweipeng hongweipeng closed this Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants